package com.changer.common.vo;

import java.io.Serializable;

import com.changer.common.vo.enums.ResponseStatus;
import com.changer.framework.util.GoogleJsonUtils;
import com.changer.framework.util.ReflectionUtils;

/**
 * @author Jiwei Guo
 *
 */
public class JsonResponse implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer code;

	private String msg;

	private Object body;

	public JsonResponse(){
		super();
	}

	public JsonResponse(Integer code,String msg){
		this.code = code;
		this.msg = msg;
	}
	public JsonResponse(ResponseStatus status){
		this(status.getCode(),status.getStatus());
	}

	public JsonResponse(Integer code,String msg,Object body){
		this.code = code;
		this.msg = msg;
		this.body = body;
	}

	public static JsonResponse wrapper(Object body){
		JsonResponse response = new JsonResponse(ResponseStatus.SUCCESS);
		response.setBody(body);
		return response;
	}
	public static String ok() {
		JsonResponse response = new JsonResponse();
		response.setCode(1);
		return GoogleJsonUtils.toJson(response);
	}
	
	public static String ok(Object body) {
		JsonResponse response = new JsonResponse();
		response.setCode(1);
		response.setBody(body);
		return GoogleJsonUtils.toJson(response);
	}
	
	public static String msg(Integer code, String msg){
		JsonResponse response = new JsonResponse();
		response.setCode(code);
		response.setMsg(msg);
		return GoogleJsonUtils.toJson(response);
	}

	public static String serverError() {
		JsonResponse response = new JsonResponse();
		response.setCode(-1);
		return GoogleJsonUtils.toJson(response);
	}
	
	public static String invalidRequestParams(String msg) {
		JsonResponse response = new JsonResponse();
		response.setCode(100);
		response.setMsg(msg);
		return GoogleJsonUtils.toJson(response);
	}
	
	public static String invalidToken() {
		JsonResponse response = new JsonResponse();
		response.setCode(0);
		return GoogleJsonUtils.toJson(response);
	}

	public Object getBody() {
		return body;
	}

	public void setBody(Object body) {
		this.body = body;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String toString() {
		return ReflectionUtils.toString(this);
	}
}
